#!/bin/sh
# Slackbuild for vbam

# Currently maintained by B. Watson <yalhcru@gmail.com>

# Previously maintained by Dugan Chen <email removed>

# Written by JK Wood <email removed>

# Slackbuild is released under the Dog-on-Fire License:
# If use of this script causes your dog to catch on fire,
# you agree to send me five dollars.  Or a picture
# of the dog on fire.
# Otherwise, you're on your own.  I've tested the script
# on my own computer, and it hasn't broken anything.
# So if it does it on your computer, that falls in
# the realm of "Not my problem."
#
# Of course, if you'll send a bug report to the above
# email address, I may be able to see what you did
# wrong and prevent it from happening in the future.
# In which case, I may just send YOU five dollars.

# Modified by the SlackBuilds.org project.

# 20170302 bkw: use long-format github URL

# 20170126 bkw:
# - New maintainer, remove old maintainers' email addresses.
# - Update for 2.0.0beta3. This adds SDL2 and SFML as deps.
#   Actually I could make SFML optional, it's only used for emulating
#   the GBA link cable. If anyone *really* wants to do this, add
#   -DENABLE_LINK=OFF to the cmake command. Not going to add it as
#   an option unless someone requests it.
# - Install binary in /usr/games, not /usr/bin.
# - Use correct wx-config, even if /usr/bin/wx-config symlink points
#   to 2.8 instead of 3.0.
# - Get rid of Windows-only doc.
# - Binary & source name has changed to visualboyadvance-m, keep the
#   old vbam name as symlinks.
# - Correct README, plus cosmetic reformatting.

PRGNAM=vbam
VERSION=${VERSION:-2.0.0beta3}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

SRCVER="Beta-3"
SRCNAM="visualboyadvance-m"

# Set to ON to enable FFMPEG A/V recording.
FFMPEG=${FFMPEG:-OFF}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
  ASM=ON # x86 ASM optimizations are available.
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
  ASM=ON
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
  ASM=OFF
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
  ASM=OFF
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-$SRCVER
tar xvf $CWD/$SRCNAM-$SRCVER.tar.gz
cd $SRCNAM-$SRCVER
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
 -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

# if both wx(Python|GTK) 2 and 3 are installed, /usr/bin/wx-config
# is a symlink to the config for whichever was installed last. Avoid
# confusion, force the correct version.
WXCONF=${WXCONF:-/usr/lib$LIBDIRSUFFIX/wx/config/gtk2-unicode-3.0}

# install binary to /usr/games, since it's got a section 6 man page.
sed -i '/DESTINATION bin/s,bin,games,' CMakeLists.txt

# man pages go in Slack standard location.
sed -i 's,share/man,man,g' CMakeLists.txt

mkdir -p build
cd build
  cmake \
    -DwxWidgets_CONFIG_EXECUTABLE=$WXCONF \
    -DCMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DENABLE_ASM_CORE=${ASM} \
    -DENABLE_ASM_SCALERS=${ASM} \
    -DENABLE_FFMPEG=${FFMPEG} \
    -DCMAKE_BUILD_TYPE=Release ..
  make
  make install DESTDIR=$PKG
cd ..

strip $PKG/usr/games/$SRCNAM
ln -s $SRCNAM $PKG/usr/games/$PRGNAM

gzip $PKG/usr/man/man6/$SRCNAM.6
ln -s $SRCNAM.6.gz $PKG/usr/man/man6/$PRGNAM.6.gz

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
rm -f doc/ReadMe.MFC.txt # this is for Windows only
cp -a doc/* $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
